package com.one.rope.mvp.basecore.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

/**
 * 日期工具类
 */
public class DateUtil {

  public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
  public static final String ENGLISH_FORMAT = "MMM d, yyyy h:m:s aa";
  public static final String YYYY_MM_DD = "yyyy-MM-dd";

  /**
   * @desc 日期转英文日期日期
   */
  public static String dateToEnglish(Date date) {
    if (date == null) {
      return "";
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat(ENGLISH_FORMAT, Locale.ENGLISH);
    return dateFormat.format(date);
  }

  public static Date stringToDate(String dateString) {
    return stringToDate(dateString, DEFAULT_FORMAT);
  }

  public static Date stringToDate(String dateString, String format) {
    try {
      DateFormat dateFormat = new SimpleDateFormat(format);
      return dateFormat.parse(dateString);
    } catch (ParseException e) {
      return null;
    }
  }

  /**
   * @return java.lang.String
   * @Description 日期转换字符串
   * @Author gavin(chaogai.wang @ temtop.com)
   * @Param [date, format, calendar]
   * @Date 2018/12/20 10:22
   **/
  public static String dateToString(Date date, String format, Calendar calendar) {
    try {
      if (date == null) {
        return "";
      }
      DateFormat dateFormat = new SimpleDateFormat(format);
      if (calendar != null) {
        dateFormat.setCalendar(calendar);
      }
      return dateFormat.format(date);
    } catch (Exception e) {
      return "";
    }
  }

  public static String dateToString(Date date) {
    return dateToString(date, DEFAULT_FORMAT);
  }

  //new GregorianCalendar(new SimpleTimeZone(0, "GMT"))
  public static String dateToString(Date date, String format) {
    return dateToString(date, format, null);
  }

  /**
   * @return String
   * @Author gavin(wcggavin @ gmail.com)
   * @Description 时间戳转换string
   * @Date 2019-01-11 14:17
   * @Params time, format
   **/
  public static String stampToDate(Long time, String format) {
    if (time == null) {
      return null;
    }

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
    Date date = new Date(time);
    return simpleDateFormat.format(date);
  }

  public static String stampToDate(Long time) {
    if (time == null) {
      return null;
    }
    return stampToDate(time, "yyyy-MM-dd HH:mm:ss");
  }

  /**
   * 生成日期时间+ 随机数字符串
   */
  public static String getDateRandom() {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    String str = simpleDateFormat.format(new Date());
    return str + ((int) (new Random().nextDouble() * (99999 - 10000 + 1)) + 10000);
  }

  /**
   * @Title: yearDateDiff @Description: 计算两个日期相差年数 @param @param startDate @param @param endDate
   * @param @return @return int @throws
   */
  public static int yearDateDiff(String startDate, String endDate) {
    if (startDate == null || endDate == null) {
      return 0;
    }
    Calendar calBegin = Calendar.getInstance(); // 获取日历实例
    Calendar calEnd = Calendar.getInstance();
    calBegin.setTime(stringToDate(startDate, "yyyy")); // 字符串按照指定格式转化为日期
    calEnd.setTime(stringToDate(endDate, "yyyy"));
    return calEnd.get(Calendar.YEAR) - calBegin.get(Calendar.YEAR);
  }

  public static String longToString(Long time) {
    if (time == null) {
      return "";
    }
    return longToString(time, DEFAULT_FORMAT);
  }

  public static String longToString(long time, String format) {
    Date date = new Date(time);
    return dateToString(date, format);
  }

  public static Date getNowDate() {
    SimpleDateFormat df = new SimpleDateFormat(DEFAULT_FORMAT);
    String nowTime = df.format(new Date());
    Date date = null;
    try {
      date = df.parse(nowTime);
    } catch (Exception e) {
    }

    return date;
  }

}
